<template>
  <form action="/">
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
    />
  </form>
  <van-divider>已选标签</van-divider>
  <div v-if="activeIds.length === 0"> 请选择标签 </div>
  <van-row gutter="10">
    <van-col v-for="tag in activeIds">
      <van-tag  :show="show" closeable size="medium" type="primary" @close="close(tag)">
        {{ tag }}
      </van-tag>
    </van-col>

  </van-row>


  <van-divider>选择标签</van-divider>
  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tagList"
  />
  <div style="padding: 12px;">
    <van-button block type="primary" @click="doSearchResult">搜索</van-button>
  </div>

</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Toast } from 'vant';
import {useRouter} from "vue-router";

const searchText = ref('');
//标签列表
const originTagList = [
  {
    text: '性别',
    children: [
      { text: '男', id: '男' },
      { text: '女', id: '女' },
    ],
  },
  {
    text: '年级',
    children: [
      { text: '大一', id: '大一' },
      { text: '大二', id: '大二' },
      { text: '大三', id: '大三' },
    ],
  },
];
let tagList = ref(originTagList);
/**
 * 搜索过滤
 * @param val
 */
const onSearch = (val) => {
  tagList.value = originTagList.map(parentTag => {
    const tempChildren = [...parentTag.children];
    const tempParentTag = {...parentTag};
    tempParentTag.children = tempChildren.filter(item => item.text.includes(searchText.value));
    return tempParentTag;
  });

  console.log(activeIds.value)
};
const onCancel = () => {
  searchText.value = '';
  tagList.value = originTagList;
};

//已选中标签 activeIndex左侧已选中tagList索引，activeIds右侧已选中标签索引
const activeIds = ref([]);
const activeIndex = ref(0);

//移除已选中标签
const close = (tag) => {
  activeIds.value = activeIds.value.filter((v) => v !== tag);
}
/**
 * 执行搜索
 */
const router = useRouter()
const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query: {
      tags: activeIds.value
    }
  })
}
</script>
<style scoped>

</style>